package com.imay.live.helper;

import com.alibaba.fastjson.JSON;
import com.imay.live.IMApplication;
import com.imay.live.R;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.room.RoomTips;
import com.imay.live.proto.room.RoomTipsResp;

/**
 * Created by jeaman on 2017/8/16.
 * 直播公告
 */

public class RoomNoticeHelper {
    private static RoomNoticeHelper instance;

    private RoomTips mRoomNotice;

    public synchronized static RoomNoticeHelper getInstance() {
        if (instance == null) {
            instance = new RoomNoticeHelper();
        }
        return instance;
    }

    private RoomNoticeHelper() {
        getLastNotice();
    }

    private void getLastNotice() {
        String notice = PreferenceHelper.getInstance().getRoomNotice();
        if (notice == null) {
            mRoomNotice = new RoomTips();
            mRoomNotice.setContentTitle(IMApplication.getInstance().getString(R.string.live_room_notice_title));
            mRoomNotice.setContent(IMApplication.getInstance().getString(R.string.live_room_notice_content));
        } else {
            mRoomNotice = JSON.parseObject(notice, RoomTips.class);
        }
    }

    public void requestNotice() {
        ProtoRequestHelper.task(ProtoHttpFactory.requestRoomTips()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                RoomTipsResp resp = JSON.parseObject(data, RoomTipsResp.class);
                if (resp.getResult() == ResultCode.CodeSuccess && resp.getRoomEnterTips() != null) {
                    if (resp.getRoomEnterTips().size() > 0) {
                        mRoomNotice = resp.getRoomEnterTips().get(0);
                        PreferenceHelper.getInstance().saveRoomNotice(JSON.toJSONString(mRoomNotice));
                    }
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {

            }
        });
    }

    public RoomTips getNotice() {
        return mRoomNotice;
    }
}
